<?php

namespace App\Listeners;

use App\Events\SellMember;
use App\Models\DealLog;
use App\Models\Recharge;
use App\Models\Shop;
use App\Models\ShopProfit;
use App\Models\ShopRole;
use App\Models\User;
use App\Models\Wallet;
use Illuminate\Support\Facades\DB;

class DispatchProfit
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  SellMember  $event
     * @return void
     */
    public function handle(SellMember $event)
    {
        $re_id = $event->recharge_id;

        $re_data = Recharge::where(['id' => $re_id, 'status' => 2])->first();

        if ($re_data->shop_id) {
            $shop_pro_list = Shop::where(['status' => 3, 'id' => $re_data->shop_id])->first();
        }

        // $pro_money = bcdiv((($re_data->money) * 0.25), count($shop_pro_list), 2);
        $pro_money = bcmul($re_data->money, 0.25, 2);

        $re_user = User::where('id', $re_data->user_id)->first();

        $p_id = $re_user->pid;
        $p_user_wallet = Wallet::where('user_id', $p_id)->first();
        $f_id = $re_user->fid;

        $f_user_wallet = Wallet::where('user_id', $f_id)->first();

        DB::beginTransaction();
        try {
            if ($p_id && ($f_id == 0)) { //如果只有邀请注册，没有邀请办卡，则给邀请人2%奖励
                $invite_money = bcmul($re_data->money, 0.02, 2); //分配金额
                $p_user_wallet->money = $p_user_wallet->money + $invite_money;
                $p_user_wallet->save();
                DealLog::add_log($p_id, 0, $invite_money, 1, '邀请会员奖励');
            }

            if ($f_id) {
                $invite_money = bcmul($re_data->money, 0.02, 2); //分配金额
                $f_user_wallet->money = $f_user_wallet->money + $invite_money;
                $f_user_wallet->save();
                DealLog::add_log($f_id, 0, $invite_money, 1, '邀请办卡奖励');
                // ShopRole::where(['shop_id' => $re_data->shop_id, 'user_id' => $f_id, 'status' => 1])->increment('shop_profit', $invite_money);

                $shopRole = ShopRole::where(['shop_id' => $re_data->shop_id, 'user_id' => $f_id, 'status' => 1])->first();
                if ($shopRole) {
                    $shopRole->shop_profit = $shopRole->shop_profit + $invite_money;
                    $shopRole->save();
                    ShopProfit::add_log($f_id, $re_data->shop_id, 2, $invite_money, '店铺角色收益');
                }
            }

            $re_user->fid = 0;

            if ($re_data->shop_id) {
                $re_user->shop_id = $re_data->shop_id;
                Shop::where(['status' => 3, 'id' => $re_data->shop_id])->increment('shop_profit', $pro_money);
                Wallet::where('user_id', $shop_pro_list->user_id)->increment('money', $pro_money);
                ShopProfit::add_log($shop_pro_list->user_id, $re_data->shop_id, 1, $pro_money, '店铺发起人收益');
            }
            $re_user->save();
            DB::commit();
        } catch (\Exception $exception) {
            DB::rollBack();
        }
    }

}
